package com.jic.product.controller.manage;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.conf.api.manage.ProductCommentReplyApi;
import com.jic.conf.request.ProductCommentReplyRequest;
import com.jic.conf.response.ProductCommentReplyResponse;
import com.jic.product.constant.consist.ResponseCode;
import com.jic.product.exception.AppRuntimeException;
import com.jic.product.model.entity.ProductCommentReply;
import com.jic.product.service.ProductCommentReplyService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@SuppressWarnings("all")
@Slf4j
@RestController
@RequestMapping("/productCommentReply")
public class ProductCommentReplyController implements ProductCommentReplyApi {


    @Autowired
    private RedisClientTemplate redisClientTemplate;
    @Autowired
    private ProductCommentReplyService productCommentReplyService;

    @ApiOperation("商品评论回复分页查询")
    @PostMapping("/getProductCommentReplyPage")
    @Override
    public RestResult<PageResult<ProductCommentReplyResponse>> getProductCommentReplyPage(@RequestBody ProductCommentReplyRequest productCommentReplyRequest) {
        try {
            if (productCommentReplyRequest.getStart() == 0 || productCommentReplyRequest.getLimit() == 0){
                throw new Exception("商品评论分页查询失败===>>>分页参数未传");
            }
            Page page = new Page();
            page.setLimit(productCommentReplyRequest.getLimit());
            page.setStart(productCommentReplyRequest.getStart());
            ProductCommentReply productCommentReply = new ProductCommentReply();
            BeanUtils.copyProperties(productCommentReplyRequest,productCommentReply);
            PageResult<ProductCommentReplyResponse> productCommentReplyResponsePageResult = productCommentReplyService.queryPage(productCommentReply, page);
            return RestResult.success(productCommentReplyResponsePageResult);
        }catch (Exception e){
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
    }

    @ApiOperation("商品评论回复新增")
    @PostMapping("/updateProductCommentReplyInfo")
    @Override
    public RestResult<String> updateProductCommentReplyInfo(@RequestBody ProductCommentReplyRequest productCommentReplyRequest) {
        try {
            ProductCommentReply productCommentReply = new ProductCommentReply();
            BeanUtils.copyProperties(productCommentReplyRequest,productCommentReply);
            String token = productCommentReplyRequest.getToken();
            String userStr = redisClientTemplate.get(token);
            if(StringUtils.isEmpty(userStr))return RestResult.error("9999","登陆失效");
            Map<String,Object> user = JSONArray.parseObject(userStr, Map.class);
            productCommentReply.setCreateTime(new Date());
            productCommentReply.setCreator(Long.valueOf(user.get("id").toString()));
            productCommentReply.setCreatorName(user.get("username").toString());
            productCommentReplyService.insert(productCommentReply);
            return RestResult.success("成功");
        }catch (Exception e){
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
    }
}
